{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 生成处理数据\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-10-14T01:34:04.290641Z",
     "iopub.status.busy": "2023-10-14T01:34:04.290110Z",
     "iopub.status.idle": "2023-10-14T01:34:04.534465Z",
     "shell.execute_reply": "2023-10-14T01:34:04.533272Z",
     "shell.execute_reply.started": "2023-10-14T01:34:04.290609Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "!mkdir train_data_dir\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2023-10-14T01:34:10.029073Z",
     "iopub.status.busy": "2023-10-14T01:34:10.028515Z"
    },
    "jupyter": {
     "outputs_hidden": false
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "744915it [00:05, 130972.12it/s]\r\n",
      "100%|██████████| 3/3 [00:00<00:00, 12470.68it/s]3it/s]\r\n",
      "100%|██████████| 93114/93114 [05:12<00:00, 298.35it/s]\r\n",
      "100%|██████████| 93114/93114 [05:16<00:00, 294.39it/s]\r\n",
      "100%|██████████| 93114/93114 [05:19<00:00, 291.79it/s]\r\n",
      "100%|██████████| 93114/93114 [05:21<00:00, 289.40it/s]\r\n",
      "100%|██████████| 93114/93114 [05:23<00:00, 288.01it/s]\r\n",
      "100%|██████████| 93114/93114 [05:23<00:00, 288.17it/s]\r\n",
      "100%|██████████| 93114/93114 [05:23<00:00, 287.93it/s] \r\n",
      "100%|██████████| 93114/93114 [05:25<00:00, 285.99it/s] \r\n",
      "100%|██████████| 3/3 [00:00<00:00, 29.56it/s]it/s]]\r\n",
      " 67%|██████▋   | 62429/93114 [18:33<08:12, 62.34it/s]  "
     ]
    }
   ],
   "source": [
    "# 生成token表\n",
    "\n",
    "# import jieba\n",
    "import pandas as pd\n",
    "from tqdm import tqdm\n",
    "import numpy as np\n",
    "from glob import glob\n",
    "from multiprocessing import Process, Manager, freeze_support\n",
    "\n",
    "\n",
    "def gen_text_cut_by_jieba(one_data_in, return_voc):\n",
    "    voc_id = []\n",
    "    for i in tqdm(one_data_in):\n",
    "        # voc_id += list(set(list(jieba.cut(i))))\n",
    "        voc_id += list(set(list(i)))\n",
    "        voc_id = list(set(voc_id))\n",
    "    return_voc.append(voc_id)\n",
    "\n",
    "\n",
    "def gen_data_pre():\n",
    "    with open(\"/home/aistudio/data/data110057/fixed_couplets_in.txt\", encoding=\"utf-8\") as f:\n",
    "        in_data = f.readlines()\n",
    "    with open(\"/home/aistudio/data/data110057/fixed_couplets_out.txt\", encoding=\"utf-8\") as f:\n",
    "        out_data = f.readlines()\n",
    "    src_data = [[\"<aos>\"] + list(\n",
    "        \"\".join(i.strip().split())) + [\"<bos>\", \"<cos>\"] + list(\"\".join(j.strip().split())) + [\"<dos>\"] for i, j in\n",
    "        tqdm(zip(in_data, out_data))]\n",
    "    src_data = pd.DataFrame(src_data).fillna(\"<eos>\")\n",
    "    \n",
    "    pd.to_pickle({\"data\": src_data.values.tolist()}, \"src_data.pandas_pickle\")\n",
    "\n",
    "\n",
    "def gen_voc_to_pandas_pickle():\n",
    "  \n",
    "\n",
    "    src_data = pd.read_pickle(\"src_data.pandas_pickle\")[\"data\"]\n",
    "    work_num = 8\n",
    "    works_num_steps = len(src_data) // work_num\n",
    "    voc_id_list = Manager().list()\n",
    "    p_list = []\n",
    "    for i in range(0, len(src_data), works_num_steps):\n",
    "        # for i in range(0, 24, work_num):\n",
    "        j = i + works_num_steps\n",
    "        one_data = src_data[i:j]\n",
    "        p = Process(target=gen_text_cut_by_jieba, args=(one_data, voc_id_list))\n",
    "        p.start()\n",
    "        p_list.append(p)\n",
    "    for p in p_list:\n",
    "        p.join()\n",
    "    total_list=list(set(np.hstack(voc_id_list)))\n",
    "    pd.to_pickle({\"data\":total_list}, \"voc_data1.pandas_pickle\")\n",
    "    voc_id_list = [\"<pad>\"]+total_list\n",
    "    pd.to_pickle({\"data\": voc_id_list}, \"voc_data.pandas_pickle\")\n",
    "\n",
    "\n",
    "def gen_text_cut_by_jieba_to_token(src_data_s, one_voc, out_dir, start_index, end_index):\n",
    "    voc_df = pd.DataFrame({\"voc\": one_voc})\n",
    "    voc_df[\"voc_id\"] = voc_df.index.values\n",
    "    total_list = []\n",
    "    fill_data=voc_df.loc[voc_df[\"voc\"]==\"<dos>\",\"voc_id\"].values.tolist()[0]\n",
    "    for src_data_one in tqdm(src_data_s):\n",
    "        # one_voc = pd.DataFrame({\"voc\": list(jieba.cut(src_data_one))})\n",
    "        one_voc = pd.DataFrame({\"voc\": list(src_data_one)})\n",
    "        new_voc_df = voc_df[voc_df[\"voc\"].isin(one_voc[\"voc\"])]\n",
    "        # print(fill_data)\n",
    "        one_voc = pd.merge(one_voc, new_voc_df, on=\"voc\", how=\"left\").fillna(fill_data)\n",
    "        total_list.append(one_voc[\"voc_id\"].values.tolist())\n",
    "        # 如何太大可以每隔n个进行保存\n",
    "    pd.to_pickle({\"data\": total_list}, \"{}/{}{}.pandas_pickle\".format(out_dir, start_index, end_index))\n",
    "\n",
    "\n",
    "def gen_src_data_to_token():\n",
    "    src_data = pd.read_pickle(\"src_data.pandas_pickle\")[\"data\"]\n",
    "    voc_id = pd.read_pickle(\"voc_data.pandas_pickle\")[\"data\"]\n",
    "    work_num = 8\n",
    "    works_num_steps = len(src_data) // work_num\n",
    "\n",
    "    p_list = []\n",
    "    try:\n",
    "        for i in range(0, len(src_data), works_num_steps):\n",
    "            # for i in range(0, 24, work_num):\n",
    "            j = i + works_num_steps\n",
    "            one_data = src_data[i:j]\n",
    "            p = Process(target=gen_text_cut_by_jieba_to_token,\n",
    "                        args=(one_data, voc_id, \"/home/aistudio/train_data_dir\", \"len_{}_{}_\".format(i,i), j))\n",
    "            p.start()\n",
    "            p_list.append(p)\n",
    "        for p in p_list:\n",
    "            p.join()\n",
    "    except:\n",
    "        print()\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    freeze_support()\n",
    "    # data=pd.read_pickle(\"voc_data.pandas_pickle\")\n",
    "    # print()\n",
    "    # 数据预处理\n",
    "    gen_data_pre()\n",
    "    # 生成token表\n",
    "    gen_voc_to_pandas_pickle()\n",
    "\n",
    "    gen_src_data_to_token()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 定义网络"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-10-14T06:32:16.080779Z",
     "iopub.status.busy": "2023-10-14T06:32:16.080381Z",
     "iopub.status.idle": "2023-10-14T06:32:17.603694Z",
     "shell.execute_reply": "2023-10-14T06:32:17.602901Z",
     "shell.execute_reply.started": "2023-10-14T06:32:16.080749Z"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "import paddle\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from tqdm import tqdm\n",
    "\n",
    "\n",
    "class FeedFroward(paddle.nn.Layer):\n",
    "    def __init__(self, hidden_dim):\n",
    "        super(FeedFroward, self).__init__()\n",
    "        self.fc1 = paddle.nn.Linear(hidden_dim, hidden_dim // 2, bias_attr=False)\n",
    "        self.fc2 = paddle.nn.Linear(hidden_dim // 2, hidden_dim, bias_attr=False)\n",
    "\n",
    "    def forward(self, feed_x):\n",
    "        feed_x = self.fc1(feed_x)\n",
    "        # sin cos 一样 其次是silu 和tanh\n",
    "        feed_x = paddle.sin(feed_x)\n",
    "        feed_x = self.fc2(feed_x)\n",
    "        return feed_x\n",
    "\n",
    "\n",
    "class CVFroward(paddle.nn.Layer):\n",
    "    def __init__(self, hidden_dim, k_size):\n",
    "        super(CVFroward, self).__init__()\n",
    "        self.fc1 = paddle.nn.Conv1D(in_channels=hidden_dim, out_channels=hidden_dim // 2, bias_attr=False,\n",
    "                                    kernel_size=k_size, stride=1)\n",
    "        self.fc2 = paddle.nn.Conv1D(in_channels=hidden_dim // 2, out_channels=hidden_dim, bias_attr=False,\n",
    "                                    kernel_size=k_size, stride=1)\n",
    "\n",
    "        self.pad = k_size - 1\n",
    "\n",
    "    def forward(self, feed_x):\n",
    "        feed_x = feed_x.transpose([0, 2, 1])\n",
    "        zero_pad = paddle.zeros(shape=[feed_x.shape[0], feed_x.shape[1], self.pad], dtype=feed_x.dtype)\n",
    "        feed_x = paddle.concat([zero_pad, feed_x], -1)\n",
    "        feed_x = self.fc1(feed_x)\n",
    "        # sin cos 一样 先是silu 和tanh\n",
    "        feed_x = paddle.sin(feed_x)\n",
    "        zero_pad = paddle.zeros(shape=[feed_x.shape[0], feed_x.shape[1], self.pad], dtype=feed_x.dtype)\n",
    "\n",
    "        feed_x = paddle.concat([zero_pad, feed_x], -1)\n",
    "        feed_x = self.fc2(feed_x)\n",
    "        feed_x = feed_x.transpose([0, 2, 1])\n",
    "\n",
    "        return feed_x\n",
    "\n",
    "\n",
    "class CVF(paddle.nn.Layer):\n",
    "    def __init__(self, voc_size, hidden_dim, n_layers, k_size):\n",
    "        super(CVF, self).__init__()\n",
    "\n",
    "        self.emx = paddle.nn.Embedding(voc_size, hidden_dim)\n",
    "        self.cv_list = paddle.nn.LayerList(\n",
    "            [CVFroward(hidden_dim, k_size) for _ in range(n_layers)])\n",
    "        self.feed_list = paddle.nn.LayerList(\n",
    "            [FeedFroward(hidden_dim) for _ in range(n_layers)])\n",
    "\n",
    "        self.out_layer = paddle.nn.Linear(hidden_dim, voc_size)\n",
    "        # 添加残差连接和层归一化\n",
    "        self.norm1 = paddle.nn.LayerNorm(hidden_dim)\n",
    "\n",
    "    def forward(self, x):\n",
    "        x = self.emx(x)\n",
    "        ll = x\n",
    "\n",
    "        for cv, feed in zip(self.cv_list, self.feed_list):\n",
    "            ll += cv(ll)\n",
    "            ll = self.norm1(ll)\n",
    "            ll += feed(ll)\n",
    "            ll = self.norm1(ll)\n",
    "\n",
    "        outl = self.out_layer(ll)\n",
    "        return outl\n",
    "\n",
    "\n",
    "def emheading_train_and_sample():\n",
    "    print(\"*\" * 100)\n",
    "\n",
    "    # net = ReNetAll(12935, 256, 8)\n",
    "    net = CVF(1235, 128, 6, 8)\n",
    "\n",
    "    # net.eval()\n",
    "    x = paddle.to_tensor([\n",
    "        np.random.randint(1, 1140, 5120),\n",
    "        np.random.randint(1, 1140, 5120),\n",
    "    ], dtype='int64')\n",
    "\n",
    "    # 模拟训练\n",
    "\n",
    "    loss_f = paddle.nn.CrossEntropyLoss()\n",
    "\n",
    "    opt = paddle.optimizer.Adam(parameters=net.parameters(), learning_rate=0.0003)\n",
    "    bar = tqdm(range(1260))\n",
    "    for epoch in bar:\n",
    "\n",
    "        for i in range(0, x.shape[-1], 256):\n",
    "            j = i + 256\n",
    "\n",
    "            outl = net(x[:, i + 1:j - 1])\n",
    "            loss = loss_f(outl, x[:, i + 2:j])\n",
    "\n",
    "            bar.set_description(\"epoch---{}--loss--{:.5f}\".format(epoch, loss.item()))\n",
    "\n",
    "            loss.backward()\n",
    "        opt.step()\n",
    "        opt.clear_grad()\n",
    "    # 解码，验证\n",
    "    net.eval()\n",
    "    state_l = paddle.zeros([1])\n",
    "\n",
    "    for i in range(0, x.shape[-1], 256):\n",
    "        j = i + 256\n",
    "\n",
    "        out0, out1, state_l = net(x[:, i:j - 1], x[:, i + 1:j], paddle.to_tensor(state_l.numpy()))\n",
    "\n",
    "\n",
    "# 进行模型训练和预测\n",
    "# if __name__ == '__main__':\n",
    "#     emheading_train_and_sample()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-10-14T00:18:28.052715Z",
     "iopub.status.busy": "2023-10-14T00:18:28.052226Z"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "W1014 08:18:28.073454   152 gpu_resources.cc:119] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 12.0, Runtime API Version: 11.6\r\n",
      "W1014 08:18:28.080693   152 gpu_resources.cc:149] device: 0, cuDNN Version: 8.4.\r\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "加载成功\r\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1/1 [00:00<00:00, 714.05it/s]\r\n",
      "100%|██████████| 1/1 [00:01<00:00,  1.18s/it]\r\n",
      "100%|██████████| 1/1 [00:01<00:00,  1.30s/it]\r\n",
      "100%|██████████| 1/1 [00:01<00:00,  1.40s/it]\r\n",
      "100%|██████████| 1/1 [00:01<00:00,  1.42s/it]\r\n",
      "100%|██████████| 1/1 [00:01<00:00,  1.51s/it]\r\n",
      "100%|██████████| 1/1 [00:01<00:00,  1.47s/it]\r\n",
      "100%|██████████| 1/1 [00:01<00:00,  1.39s/it]\r\n",
      "100%|██████████| 1/1 [00:01<00:00,  1.41s/it]\r\n",
      "  0%|          | 0/32400 [00:00<?, ?it/s]"
     ]
    }
   ],
   "source": [
    "import os.path\n",
    "from multiprocessing import Process, Manager, freeze_support\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "# from cv_net_for_nlp import CVF\n",
    "\n",
    "from glob import glob\n",
    "# import jieba\n",
    "import warnings\n",
    "import paddle\n",
    "from tqdm import tqdm\n",
    "\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "\n",
    "\n",
    "def son_process_of_train_net(epoch, bar, one_data, max_len, loss_avg, net, loss_f, opt, steps):\n",
    "    if (len(loss_avg) + 1) % 1000 == 0:\n",
    "        # print(np.mean(loss_avg), \"____\", np.mean(acc_avg))\n",
    "        paddle.save(net.state_dict(), \"long_attention_model\")\n",
    "        paddle.save({\"data\": loss_avg}, \"loss_avg\")\n",
    "\n",
    "    one_data = np.array(one_data)\n",
    "    # print(one_data.shape)\n",
    "    label_data = one_data[:, 1:]\n",
    "    in_data = one_data[:, :-1]\n",
    "\n",
    "    i_data = paddle.to_tensor(in_data)\n",
    "    l_data = paddle.to_tensor(label_data)\n",
    "\n",
    "    # print(i_data[0], l_data[0])\n",
    "\n",
    "    # 训练代码写这里\n",
    "\n",
    "    outl = net(i_data.astype(\"int64\"))\n",
    "    loss = loss_f(outl, l_data.astype(\"int64\"))\n",
    "\n",
    "    acc = np.mean((paddle.argmax(outl, -1)[:, :].reshape([-1]) == l_data[:, :].reshape([-1])).numpy())\n",
    "    # loss = loss_f(out, label.reshape([-1]).astype(\"int64\"))\n",
    "    # acc = np.mean((paddle.argmax(out, -1) == label.reshape([-1])).numpy())\n",
    "\n",
    "    loss_data = loss.item()\n",
    "\n",
    "    loss_avg.append(loss_data)\n",
    "    bar.set_description(\n",
    "        desc=\"{}{}{}{}{:.5f}\".format(epoch, \"____\", \"steps___{}____\".format(steps),\n",
    "                                     \"loss___{:.5f}___\".format(np.mean(loss_avg)), acc))\n",
    "    return loss\n",
    "\n",
    "\n",
    "\n",
    "def read_one_pickle(read_path, data_voc):\n",
    "    one_set = []\n",
    "\n",
    "    for one_v in tqdm(read_path):\n",
    "        one_data = pd.read_pickle(one_v)\n",
    "        one_data = one_data[\"data\"]\n",
    "        one_set += one_data\n",
    "\n",
    "    data_voc.append(one_set)\n",
    "\n",
    "\n",
    "def load_data_to_mem(paths_list_r, voc_id, works_num):\n",
    "    resv_data = Manager().list()\n",
    "\n",
    "    np.random.shuffle(paths_list_r)\n",
    "    p_list = []\n",
    "    # 发任务到异步进程\n",
    "    for i in range(0, len(paths_list_r), len(paths_list_r) // works_num):\n",
    "        j = len(paths_list_r) // works_num + i\n",
    "\n",
    "        p = Process(target=read_one_pickle, args=(paths_list_r[i:j], resv_data))\n",
    "        p.start()\n",
    "        p_list.append(p)\n",
    "\n",
    "    for p in p_list:\n",
    "        p.join()\n",
    "    new_data = []\n",
    "    for i in tqdm(list(resv_data)):\n",
    "        new_data += i\n",
    "    return {\"data\": new_data}\n",
    "\n",
    "\n",
    "def train_net_multi_process(paths_list_r):\n",
    "    voc_id = pd.read_pickle(\"/home/aistudio/voc_data.pandas_pickle\")[\"data\"]\n",
    "\n",
    "    net = CVF(len(voc_id), 128, 3, 6)\n",
    "    # net.load_dict(paddle.load(\"long_attention_model\"))\n",
    "    # net.eval()\n",
    "    print(\"加载成功\")\n",
    "    opt = paddle.optimizer.Adam(parameters=net.parameters(), learning_rate=0.0003)\n",
    "    loss_f = paddle.nn.CrossEntropyLoss(ignore_index=0)\n",
    "    # loss_f = HeadLoss()\n",
    "\n",
    "    max_len = 256\n",
    "    works_num = 8\n",
    "    batch_size = 50\n",
    "    epochs = 54 * 10 * 60\n",
    "\n",
    "   \n",
    "    \n",
    "\n",
    "    data_sets = load_data_to_mem(paths_list_r, voc_id, works_num)[\"data\"]\n",
    "\n",
    "       \n",
    "    # print(data_sets[:10])\n",
    "\n",
    "    bar = tqdm(range(epochs))\n",
    "    loss_avg = []\n",
    "    for epoch in bar:\n",
    "   \n",
    "        \n",
    "\n",
    "        # print(data_sets)\n",
    "        np.random.shuffle(data_sets)\n",
    "        for i in range(0, len(data_sets), batch_size):\n",
    "            count = i + batch_size\n",
    "            one_data = data_sets[i:count]\n",
    "            loss=son_process_of_train_net(epoch, bar, one_data, max_len, loss_avg, net, loss_f, opt,\n",
    "                                     str(count) + \"_{}\".format(len(data_sets[0])))\n",
    "            opt.clear_grad()\n",
    "            loss.backward()\n",
    "            opt.step()\n",
    "            \n",
    "\n",
    "    # 回收异步进程\n",
    "    paddle.save(net.state_dict(), \"long_attention_model\")\n",
    "    paddle.save({\"data\": loss_avg}, \"loss_avg\")\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    freeze_support()\n",
    "    train_net_multi_process(glob(\"/home/aistudio/train_data_dir/*\"))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 使用模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-10-14T06:32:28.193748Z",
     "iopub.status.busy": "2023-10-14T06:32:28.192687Z",
     "iopub.status.idle": "2023-10-14T06:32:32.330807Z",
     "shell.execute_reply": "2023-10-14T06:32:32.329822Z",
     "shell.execute_reply.started": "2023-10-14T06:32:28.193714Z"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['<aos>', '沪', '海', '潮', '音', '，', '振', '古', '如', '斯', '，', '百', '谷', '三', '山', '雄', '首', '刹', '<bos>', '<cos>', '曹', '溪', '法', '脉', '，', '于', '今', '不', '绝', '，', '一', '花', '五', '叶', '绍', '真', '传', '<dos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>', '<eos>']\r\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "W1014 14:32:28.754904 295964 gpu_resources.cc:119] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 12.0, Runtime API Version: 11.6\r\n",
      "W1014 14:32:28.762013 295964 gpu_resources.cc:149] device: 0, cuDNN Version: 8.4.\r\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<eos>\r\n",
      "<aos>沪海潮音，振古如斯，百谷三山雄首刹<bos><cos>曹溪法脉，于今不绝，一花五叶绍真传<dos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><aos>学到手方能自主<bos><cos>出于情不可人流<dos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><aos>一代诗宗，稷契其怀，年高竟作孤舟客<bos><cos>半生杜曲，巢由非志，鬓白犹存老骥心<dos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><aos>窈窕<bos><cos>朦胧<dos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos><eos\r\n"
     ]
    }
   ],
   "source": [
    "\n",
    "import paddle\n",
    "import pandas as pd\n",
    "# from cv_net_for_nlp import CVF\n",
    "\n",
    "voc_id = pd.read_pickle(\"voc_data.pandas_pickle\")[\"data\"]\n",
    "for i in voc_id:\n",
    "    try:\n",
    "        str(i)\n",
    "    except:\n",
    "        print(i)\n",
    "voc = pd.read_pickle(\"/home/aistudio/train_data_dir/len_279342_279342_372456.pandas_pickle\")[\"data\"]\n",
    "\n",
    "# print(voc_id)\n",
    "print([voc_id[i] for i in voc[0]])\n",
    "\n",
    "net = CVF(len(voc_id), 128, 3, 6)\n",
    "net.load_dict(paddle.load(\"/home/aistudio/work/long_attention_model\"))\n",
    "net.eval()\n",
    "pre_in = paddle.to_tensor(voc[:7]).reshape([1, -1]).astype(\"int64\")\n",
    "while True:\n",
    "    out_l = net(pre_in)\n",
    "    prob = out_l.exp()\n",
    "    # word_id，通过torch_multinomial，以prob为权重，对结果进行加权抽样，样本数为1(即num_samples)\n",
    "    word_id = paddle.multinomial(prob[:, -1], num_samples=1).item()\n",
    "    print(voc_id[word_id])\n",
    "    if voc_id[word_id]==\"。\" or voc_id[word_id]==\"<eos>\" :\n",
    "        break\n",
    "    pre_in = paddle.concat([pre_in, paddle.argmax(out_l, -1)[:, -1:]], -1)\n",
    "\n",
    "print(\"\".join([voc_id[i] for i in pre_in[0]]))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请点击[此处](https://ai.baidu.com/docs#/AIStudio_Project_Notebook/a38e5576)查看本环境基本用法.  <br>\n",
    "Please click [here ](https://ai.baidu.com/docs#/AIStudio_Project_Notebook/a38e5576) for more detailed instructions. "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "py35-paddle1.2.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
